package com.doeis.uwork.common;



import java.io.UnsupportedEncodingException;

import com.doeis.common.utils.DateUtils;
import com.doeis.minaserver.packet.Packet;
import com.doeis.minaserver.packet.Packet.Direction;
import com.doeis.minaserver.packet.Packet.PacketType;
import com.doeis.minaserver.packet.Packet.ProtocolVersion;
import com.doeis.minaserver.packet.Packet.TransType;
import com.doeis.uwork.entity.mongo.MinaPacketLog;
import com.doeis.uwork.entity.mongo.OffPacket;
import com.doeis.uwork.packet.GroupMsgPacket;
import com.voland.core.utils.IDGenerator;

public class PacketUtil {
	
	
	public static OffPacket cloneOffPacket(Packet packet, String userId, String bodyid ){
		OffPacket offPacket = new OffPacket();
		offPacket.setId(IDGenerator.uuid());
		offPacket.setBody(packet.getBody());
		offPacket.setCmd(packet.getCmd());
		offPacket.setDirection(packet.getDirection().ordinal());
		offPacket.setFrom(packet.getFrom());
		offPacket.setNotifyType(packet.getNotifyType());
		offPacket.setPacketType(packet.getPacketType().ordinal());
		offPacket.setPwd(packet.getPwd());
		offPacket.setTime(packet.getTime());
		offPacket.setTo(packet.getTo());
		offPacket.setTransType(packet.getTransType().ordinal());
		offPacket.setVersion(packet.getVersion().ordinal());
		offPacket.setUserId(userId);
		offPacket.setCreated_at(DateUtils.currentDateTime());
		offPacket.setBodyid(bodyid);
		offPacket.setSendCount(1);
		return offPacket;
	}
	
	
	public static MinaPacketLog cloneMinaPackett(Packet packet, boolean send){
		MinaPacketLog minaPacket = new MinaPacketLog();
		minaPacket.setId(IDGenerator.uuid());
		String body = packet.getBody();
		
		if(body!=null){
			try {
				minaPacket.setBodysize(body.getBytes("UTF-8").length);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			
			if( body.length()> 200 ){
				body = body.substring(0, 200);
			}
			
		}
		minaPacket.setBody( body);
		minaPacket.setCmd(packet.getCmd());
		minaPacket.setDirection(packet.getDirection().ordinal());
		minaPacket.setFrom(packet.getFrom());
		minaPacket.setNotifyType(packet.getNotifyType());
		minaPacket.setPacketType(packet.getPacketType().ordinal());
		minaPacket.setPwd(packet.getPwd());
		minaPacket.setTime(packet.getTime());
		minaPacket.setTo(packet.getTo());
		minaPacket.setTransType(packet.getTransType().ordinal());
		minaPacket.setVersion(packet.getVersion().ordinal());
		minaPacket.setCreated_at(DateUtils.currentDateTime());
		minaPacket.setSend(send);
		
		minaPacket.setSize(packet.getLength());
		return minaPacket;
	}
	
	
	
	public static GroupMsgPacket clonePacket(OffPacket offPacket){
		GroupMsgPacket packet = new GroupMsgPacket();
		packet.setBody(offPacket.getBody());
		packet.setCmd(offPacket.getCmd());
		packet.setDirection(Direction.values()[offPacket.getDirection()]);
		packet.setFrom(offPacket.getFrom());
		packet.setNotifyType(offPacket.getNotifyType());
		packet.setPacketType(PacketType.values()[offPacket.getPacketType()]);
		packet.setPwd(offPacket.getPwd());
		packet.setTime(offPacket.getTime());
		packet.setTo(offPacket.getTo());
		packet.setTransType(TransType.values()[offPacket.getTransType()]);
		packet.setVersion(ProtocolVersion.values()[offPacket.getVersion()]);
		return packet;
	}

}
